﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Pencas.Models
{
    public class DAOToken
    {
        private string connectionString = ConfigurationManager.ConnectionStrings["mydb"].ToString();//"Data Source=GABRIEL-PC;Initial Catalog=Pencas;Integrated Security=True; MultipleActiveResultSets=True;";

        public void GenerarTokens(int cantidad)
        {
            SQLServerConnection connection = SQLServerConnection.GetInstance(connectionString);
            string codigo = "";
            for (int i = 0; i < cantidad; i++)
            {
                Guid g = Guid.NewGuid();
                codigo = g.ToString().Substring(0,6);
                connection.ExecuteNonQuery(string.Format("INSERT INTO Token(Token) VALUES('{0}')", codigo));
            }
        }

        public DataTable ObtenerTokensDeUsuario(string email)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand comm = new SqlCommand("ObtenerToken", conn);
            comm.Parameters.Add("@email", SqlDbType.VarChar, 50);
            comm.Parameters[0].Value = email;

            SqlDataReader rdr;
            comm.CommandType = CommandType.StoredProcedure;

            conn.Open();

            rdr = comm.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Columns.Add("Token");

            while (rdr.Read())
            {
                DataRow partidoRow = dt.NewRow();
                partidoRow["Token"] = rdr["token"];
                
                dt.Rows.Add(partidoRow);
            }

            conn.Close();

            return dt;
        }
    }
}